//
//  BMARootViewController_iPad.m
//  Autismus
//
//  Created by Nicolas CHENG on 03/12/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "BMARootViewController_iPad.h"

#import "BMAMemoriExoSelectLevelViewController.h"
#import "BMAIntrusExoViewController.h"
#import "BMAQuestionsExoViewController.h"
#import "BMAEmotionViewController.h"
#import "BMAStatsViewController.h"
#import "BMAExercice.h"

@implementation BMARootViewController_iPad

@synthesize emotionButton, popupPurchaseView, purchaseManager;
@synthesize intruButton, devinetteButton;

- (id)init
{
    self = [super init];
    if (self)
    {
        self.purchaseManager = [[InAppPurchaseManager alloc] init];
        [self.purchaseManager setDelegate:self];
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self.purchaseManager];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - IBActions

- (IBAction)memoriAction:(id)sender
{
    BMAMemoriExoSelectLevelViewController *vc = [[BMAMemoriExoSelectLevelViewController alloc] init];
    vc.title = @"Sélection du Level";
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)intrusAction:(id)sender
{
    BMAExercice *exo = [[BMAAppManager sharedInstance] exerciceForIndex:1];
    if (exo.enabled == YES)
    {
        BMAIntrusExoViewController *vc = [[BMAIntrusExoViewController alloc] init];
        vc.title = @"Intrus";
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        [self.view addSubview:self.popupPurchaseView];
    }
}

- (IBAction)questionsAction:(id)sender
{
    BMAExercice *exo = [[BMAAppManager sharedInstance] exerciceForIndex:2];
    if (exo.enabled == YES)
    {
        BMAQuestionsExoViewController *vc = [[BMAQuestionsExoViewController alloc] init];
        vc.title = @"Questions";
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        [self.view addSubview:self.popupPurchaseView];
    }
}

- (IBAction)emotionsAction:(id)sender
{
    BMAEmotionViewController *vc = [[BMAEmotionViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)parentsAction:(id)sender
{
    BMAStatsViewController *vc = [[BMAStatsViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];    
}

- (IBAction)responsePopUpPurchase:(UIButton *)sender
{
    if (!sender.tag)
    {
        [purchaseManager requestProUpgradeProductData];
    }

    [self.popupPurchaseView removeFromSuperview];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"Accueil";
    
    [self initGames];
    
    [UIView animateWithDuration:0.5 animations:^{
        CGRect rect = self.emotionButton.frame;
        rect.origin.y -= 100; 
        self.emotionButton.frame = rect;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 delay:0.95 options:UIViewAnimationCurveLinear animations:^{
            CGRect rect = self.emotionButton.frame;
            rect.origin.y += 100; 
            self.emotionButton.frame = rect;
        } completion:^(BOOL finished) {
            
        }];
    }];
}

- (void)initGames
{
    {
        // If intrus game is not enabled then we put the right image
        BMAExercice *exo = [[BMAAppManager sharedInstance] exerciceForIndex:1];
        if (exo.enabled == NO) {
            [self.intruButton setBackgroundImage:[UIImage imageNamed:@"menu_intrus_H.png"] forState:UIControlStateNormal];
        } else {
            [self.intruButton setBackgroundImage:[UIImage imageNamed:@"menu_intrus"] forState:UIControlStateNormal];
        }
    }
    
    {
        // If devinette game is not enabled then we put the right image
        BMAExercice *exo = [[BMAAppManager sharedInstance] exerciceForIndex:2];
        if (exo.enabled == NO) {
            [self.devinetteButton setBackgroundImage:[UIImage imageNamed:@"menu_devinettes_H.png"] forState:UIControlStateNormal];
        } else {
            [self.devinetteButton setBackgroundImage:[UIImage imageNamed:@"menu_devinettes.png"] forState:UIControlStateNormal];
        }
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

@end
